package campshop.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import campshop.models.SchoolBuildingRegistered;
public class SchoolBuildingRegisteredDao extends JdbcDaoSupport{
	public long addSchoolBuildingRegistered(SchoolBuildingRegistered sbr)
	{
		String sql = "insert into " +
				"tbl_school_building_registered (sbr_sBuildingName, sr_uSchoolId, sbr_dCreateDate)" +
				" values (?, ?, ?)";
		if(super.getJdbcTemplate().update(sql, sbr.getsBuildingName(), sbr.getuSchoolId(), sbr.getdCreateDate())>0){
			return super.getJdbcTemplate().queryForInt("select max(sbr_uId) from tbl_school_building_registered");
		}else return 0L;
	}
	
	public boolean delSchoolBuildingRegistered(long uId)
	{
		String sql = "delete from tbl_school_building_registered where sbr_uId = ?";
		if(super.getJdbcTemplate().update(sql, uId)>0) return true;
		return false;
	}

	public SchoolBuildingRegistered getSchoolBuildingRegistered(long uId)
	{
		String sql = "select * from tbl_school_building_registered where sbr_uId=" + uId;
		return (SchoolBuildingRegistered)super.getJdbcTemplate().queryForObject(sql, new SchoolBuildingRegisteredRowMapper());
	}
	
	public List<SchoolBuildingRegistered> getSchoolBuildingList(long uSchoolId) {
		String sql = "select * from tbl_school_building_registered where sr_uSchoolId="+ uSchoolId + "order by sbr_dCreateDate desc";
		return super.getJdbcTemplate().query(sql, new SchoolBuildingRegisteredRowMapper());
	}
	
	
	class SchoolBuildingRegisteredRowMapper implements RowMapper<SchoolBuildingRegistered>{
		@Override
		public SchoolBuildingRegistered mapRow(ResultSet rs, int rowNum) throws SQLException {
			SchoolBuildingRegistered sbr = new SchoolBuildingRegistered();
			sbr.setuId(rs.getLong("sbr_uId"));
			sbr.setsBuildingName(rs.getString("sbr_sBuildingName"));
			sbr.setuSchoolId(rs.getLong("sr_uSchoolId"));
			sbr.setdCreateDate(rs.getDate("sbr_dCreateDate"));
			return sbr;
		}	
	}
}